<?php
/**
 * todu
 *
 * @copyright Copyright (c) 2018 Create-Chain PHP Group
 * @author    atangege
 */

namespace App\Http\Controllers;

use App\Exports\UsersExport;
use App\Imports\OrderImport;
use App\Model\Order;
use App\Model\Product;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;

class TestController extends Controller
{
    const PATH = '';

    public function export()
    {
        $diffSkuArray = ['i-369b7e48', 'i-32ab5f32', 'i-2289ab50'];
        $newData = [];
        $orderList = Order::all(['detail', 'code'])->toArray();

        foreach ($orderList as $k => &$v) {
            $detail = json_decode($v['detail'], true);

            foreach ($detail as $key => $value) {

                if (isset($value['commodity_code'])) {

                    if (in_array($value['commodity_code'], $diffSkuArray)) {
                        $newData[] = $v['code'];
                    }
                }
            }
        }

        if (empty($newData)) {
            echo '没有数据';
            die;
        }

        return Excel::download(new UsersExport($newData), 'tt.xlsx');
    }

    public function import()
    {
        Excel::import(new OrderImport(), self::PATH);
    }

    public function wanzi(Request $request)
    {
        $search = $request->get('name');
        $result = Product::query()->where('product_name', 'like', '%'.$search.'%')->get(['product_name', 'sale_count', 'status']);

        if (null === $result) {
            echo '00000';
        }

        $data = $result->toArray();

        return new JsonResponse(['code' => 1, 'data' => $data]);
    }
}

